home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / doSetFluidAttrFromCurve.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  10.8 KB  |  382 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  Oct 21, 2002
  22. //
  23. //  Description:
  24. //      Sets fluid properties at locations
  25. //      determined by a NURBS curve intersecting
  26. //      the voxel grid.
  27. //      
  28. //        
  29. //  Input Arguments:
  30. //      None.
  31. //
  32. //  Return Vaalue:
  33. //      None.
  34. //
  35. proc int[] voxelFromPosition( string $fluid, float $pos[], float $radius )
  36. {
  37.     int $voxels[] = `fluidVoxelInfo -radius $radius -objectSpace false -cb 
  38.                     -voxel $pos[0] $pos[1] $pos[2] $fluid`;
  39.     return $voxels;
  40. }
  41.  
  42. proc setTangentVelocity( string $fluid, string $parent,
  43.                          float $tan[], int $vox[], 
  44.                          float $value, int $absolute )
  45. //
  46. // Description:
  47. //    Set the velocity values in voxels that intersect the 
  48. //    curve (at the current curveInfo position) based on the
  49. //    tangent direction of the curve at the point.
  50. //    
  51. {
  52.     // Convert to fluid space
  53.     //
  54.     float $inverse[] = `getAttr($parent + ".worldInverseMatrix[0]")`;
  55.     $tan = pointMatrixMult( $tan, $inverse );
  56.     
  57.     float $vel[] = { $value*$tan[0], $value*$tan[1], $value*$tan[2] };
  58.  
  59.     if( !$absolute ) {
  60.         float $oldVel[] = `getFluidAttr -at "velocity" 
  61.             -xi $vox[0] -yi $vox[1] -zi $vox[2] $fluid`;
  62.  
  63.         $vel[0] = $vel[0] + $oldVel[0];
  64.         $vel[1] = $vel[1] + $oldVel[1];
  65.         $vel[2] = $vel[2] + $oldVel[2];
  66.     }
  67.  
  68.     eval( "setFluidAttr -lf -at velocity -vv " + 
  69.           $vel[0] + " " + $vel[1] + " " + $vel[2] + " " + 
  70.           " -xi " + $vox[0] + " -yi " + $vox[1] + " -zi " + $vox[2] 
  71.           + " " + $fluid );
  72.  
  73.     setAttr ($parent + ".velocityDraw") 1;
  74. }
  75.  
  76. proc setScalar( string $fluid, int $vox[], string $attr, 
  77.                 float $value, int $absolute )
  78. //
  79. // Description:
  80. //    Sets a scalar-valued attribute at the voxel intersecting
  81. //    the current curve position.
  82. //    
  83. {
  84.     int $i;
  85.     for( $i = 0; $i < size( $vox ); $i += 3 ) {
  86.         int $x = $vox[$i+0];
  87.         int $y = $vox[$i+1];
  88.         int $z = $vox[$i+2];
  89.  
  90.         float $newValue = $value;
  91.         if( !$absolute ) {
  92.             float $oldVal[] = `getFluidAttr -at $attr 
  93.                 -xi $x -yi $y -zi $z $fluid`;
  94.             
  95.             $newValue += $oldVal[0];
  96.         }
  97.  
  98.         eval( "setFluidAttr -at " + $attr + " -fv " + $newValue +
  99.               " -xi " + $x + " -yi " + $y + " -zi " + $z + 
  100.               " " + $fluid );
  101.     }
  102. }
  103.  
  104. proc setColor( string $fluid, int $vox[],
  105.                float $color[], int $absolute )
  106. //
  107. // Description:
  108. //    Sets voxel color at the intersection with the current
  109. //    curve position.
  110. //    
  111. {
  112.     if( !$absolute ) {
  113.         float $oldVal[] = `getFluidAttr -at "color"
  114.             -xi $vox[0] -yi $vox[1] -zi $vox[2] $fluid`;
  115.  
  116.         $color[0] += $oldVal[0];
  117.         $color[1] += $oldVal[1];
  118.         $color[2] += $oldVal[2];
  119.     }
  120.  
  121.     eval( "setFluidAttr -at color -vv " + 
  122.           $color[0] + " " + $color[1] + " " + $color[2] +
  123.           " -xi " + $vox[0] + " -yi " + $vox[1] + " -zi " + $vox[2] + 
  124.           " " + $fluid );
  125. }
  126.  
  127. proc addUniqueVoxelsToTargets( int $newVox[], int $voxels[], 
  128.                                float $newTan[], float $tangents[],
  129.                                string $fluid, 
  130.                                float $position[], float $distances[] )
  131. //
  132. // Description:
  133. //    Voxels could be added multiple times because of
  134. //    small parametric step values along the curve, or
  135. //    because of high radii values.  This could cause 
  136. //    problems in add mode (processing the added value
  137. //    once for each repeated occurrence).
  138. //    
  139. //    So, before adding any voxel to the list of targets to
  140. //    process, search the existing target list first to see if
  141. //    it's already there.  (This is slow due to lack of
  142. //  good array utility procs in MEL, but probably
  143. //    acceptable for most usable fluid resolutions.)
  144. //    
  145. //    We also pass in the current position and the distances 
  146. //    to that position as each voxel is processed.  This way,
  147. //    we can make sure the values stuffed into a voxel come
  148. //    from the closest current position, and not some further
  149. //    away voxel processed because of a high radius, for 
  150. //    instance.  This is necessary only for velocity tangent 
  151. //    info, since the values put into the voxels depend on
  152. //    tangent information at the closest point.
  153. //    
  154. {
  155.     int $i, $j; 
  156.     for( $i = 0; $i < size( $newVox ); $i+=3 ) {
  157.         int $found = false;
  158.  
  159.         // Get the position of the voxel center.
  160.         //
  161.         float $voxPos[] = `fluidVoxelInfo -vc -xi $newVox[$i] -objectSpace 0
  162.             -yi $newVox[$i+1] -zi $newVox[$i+2] $fluid`;
  163.         
  164.         // How far away is it from the current position
  165.         // for which we're accumulating voxels?
  166.         //
  167.         float $dist = sqrt( pow( $voxPos[0] - $position[0], 2 ) +
  168.                             pow( $voxPos[1] - $position[1], 2 ) +
  169.                             pow( $voxPos[2] - $position[2], 2 ) );
  170.         
  171.         for( $j = 0; $j < size( $voxels ); $j+=3 ) {
  172.             if( $newVox[$i+0] == $voxels[$j+0] &&
  173.                 $newVox[$i+1] == $voxels[$j+1] &&
  174.                 $newVox[$i+2] == $voxels[$j+2] )
  175.             {
  176.                 $found = true;
  177.  
  178.                 // If the voxel was already in the list, update
  179.                 // its distance values, if this voxel is closer
  180.                 // to the current position than the last current
  181.                 // position that added this particular voxel.
  182.                 //
  183.                 if( $dist < $distances[$j] ) {
  184.                     $distances[$j+0] = $dist;
  185.                     $distances[$j+1] = $dist;
  186.                     $distances[$j+2] = $dist;
  187.  
  188.                     $tangents[$j+0] = $newTan[0];
  189.                     $tangents[$j+1] = $newTan[1];
  190.                     $tangents[$j+2] = $newTan[2];
  191.                 }
  192.  
  193.                 break;
  194.             }
  195.         }
  196.  
  197.         if( !$found ) {
  198.             $voxels[ size($voxels) ] = $newVox[$i+0];
  199.             $voxels[ size($voxels) ] = $newVox[$i+1];
  200.             $voxels[ size($voxels) ] = $newVox[$i+2];
  201.  
  202.             $tangents[ size($tangents) ] = $newTan[0];
  203.             $tangents[ size($tangents) ] = $newTan[1];
  204.             $tangents[ size($tangents) ] = $newTan[2];
  205.  
  206.             $distances[ size($distances) ] = $dist;
  207.             $distances[ size($distances) ] = $dist;
  208.             $distances[ size($distances) ] = $dist;
  209.         }
  210.     }
  211.  
  212.     if( size( $voxels ) != size( $tangents ) ) {
  213.         error( "addUniqueVoxelsToTargets: array size mismatch." );
  214.     }
  215. }
  216.  
  217. global proc doSetFluidAttrFromCurve( int $version,
  218.                                      string $args[] )
  219. {
  220.     if(( $version < 1 ) || ( size( $args ) < 17 )) {
  221.         error( "Incorrect version/argument list" );
  222.         return;
  223.     }
  224.  
  225.     int   $doAttr[] = { int( $args[0] ), $args[1], $args[2], 
  226.                         $args[3], $args[4] };
  227.     float $values[] = { float($args[5]), $args[6], $args[7], $args[8], 
  228.                         $args[9], $args[10], $args[11]};
  229.     int   $absolute = ($args[12] == 2);        // 1 is relative (add)
  230.  
  231.     float $numSteps = (float) $args[13];
  232.     if( $numSteps < 1.0 ) {
  233.         $numSteps = 1.0;
  234.     }
  235.     if( $numSteps > 1000.0 ) {
  236.         $numSteps = 1000.0;
  237.     }
  238.  
  239.     float $radius = (float) $args[14];
  240.  
  241.     float $percentageStep = 1.0 / $numSteps; // have num steps,  need .001 to 1
  242.     int   $normalizeTangents = $args[15];
  243.     int   $setInitialState = $args[16];
  244.  
  245.     string $fluids[] = `ls -dag -sl -type fluidShape`;
  246.     string $curves[] = `ls -dag -sl -type nurbsCurve`;
  247.  
  248.     if(( size( $fluids ) != 1 ) || ( size( $curves ) < 1 ))
  249.     {
  250.         error( "Please select one fluid shape and at least one NURBS curve." );
  251.         return;
  252.     }
  253.  
  254.     waitCursor -state on;
  255.  
  256.     string $fluid = $fluids[0];
  257.     string $selection[] = `ls -sl`;
  258.  
  259.     string $curve;
  260.     for( $curve in $curves ) {
  261.         string $curveInfo = `createNode pointOnCurveInfo`;
  262.         string $inAttr = $curve + ".ws[0]";
  263.         string $outAttr = $curveInfo + ".ic";
  264.         connectAttr $inAttr $outAttr;
  265.         
  266.         setAttr ( $curveInfo + ".turnOnPercentage" ) true;
  267.  
  268.         // Figure out the inverse matrix of the fluid for later.
  269.         //
  270.         select $fluid;
  271.         string $parents[] = `pickWalk -d up`;
  272.         string $parent = $parents[0];
  273.         
  274.         string $attributes[] = { "density", 
  275.                                  "velocity", 
  276.                                  "temperature", 
  277.                                  "fuel",
  278.                                  "color" };
  279.  
  280.         int $whichAttr = -1;
  281.         int $whichValue = -1;
  282.         for( $attr in $attributes ) {
  283.             $whichAttr++;
  284.             $whichValue++;
  285.             if( !$doAttr[$whichAttr] ) {
  286.                 continue;
  287.             }
  288.  
  289.             string $createGridCmd[];
  290.             if( !fluidsVerifyGrid( $fluid, $attr, $createGridCmd ) ) {
  291.                 warning( "Contents Method " + $attr + " for " + $fluid + 
  292.                          " is not set to either Dynamic or Static Grid; " + 
  293.                          " skipping." );
  294.                 continue;
  295.             }
  296.  
  297.             float $value = 1.0;
  298.             if( $whichValue < size( $values ) ) {
  299.                 $value = $values[$whichValue];
  300.             }
  301.             
  302.             int $voxels[];
  303.             float $tangents[];
  304.             float $distances[];
  305.             float $i;
  306.  
  307.             // Accumulate all the voxels we'll have to set
  308.             // values for...  We can prune out the duplicates,
  309.             // and save time processing each voxel only once.
  310.             //
  311.             for( $i = 0.0; $i <= 1.0; $i += $percentageStep ) {
  312.                 setAttr ( $curveInfo + ".parameter" ) $i;
  313.  
  314.                 // Get the tangent in world space
  315.                 //
  316.                 float $newTan[];
  317.                 if( $normalizeTangents == 1 ) {    // constant
  318.                     $newTan = `getAttr($curveInfo + ".normalizedTangent")`;
  319.                 } else {                            // tangential
  320.                     $newTan = `getAttr($curveInfo + ".tangent")`;
  321.                 }
  322.  
  323.                 // Get the point position along the curve
  324.                 //
  325.                 float $curr[] = `getAttr ( $curveInfo + ".position" )`;
  326.                 int $newVox[] = voxelFromPosition( $fluid, $curr, $radius );
  327.             
  328.                 addUniqueVoxelsToTargets( $newVox, $voxels, 
  329.                                           $newTan, $tangents,
  330.                                           $fluid, $curr, $distances );
  331.             }
  332.  
  333.             int $j;
  334.             for ($j = 0; $j < size( $voxels ); $j+=3 ) {
  335.                 int $vox[] = {     $voxels[$j+0], 
  336.                                 $voxels[$j+1], 
  337.                                 $voxels[$j+2] } ;
  338.  
  339.  
  340.                 float $tan[] = { $tangents[$j+0],
  341.                                  $tangents[$j+1],
  342.                                  $tangents[$j+2] };
  343.     
  344.                 if( $attr == "velocity" ) {
  345.                     setTangentVelocity( $fluid, $parent, $tan,
  346.                                         $vox, $value, $absolute );
  347.                 } else if( $attr == "color" ) {
  348.                     float $color[] = { $value, 
  349.                                        $values[$whichValue+1], 
  350.                                        $values[$whichValue+2] };
  351.                     setColor( $fluid, $vox, $color, $absolute );
  352.                 } else {
  353.                     setScalar( $fluid, $vox, $attr, $value, $absolute );
  354.                 }
  355.             }
  356.  
  357.             // If color, bump the whichAttr counter by the two
  358.             // extra values
  359.             //
  360.             if( $attr == "color" ) {
  361.                 $whichValue+=2;
  362.             }
  363.         }
  364.  
  365.         // Cleanup and select fluid.
  366.         //
  367.         delete $curveInfo;
  368.     }
  369.  
  370.     select $selection;
  371.  
  372.     // Optionally set the initial state, if the user
  373.     // requests to, so he doesn't lose the values coming
  374.     // from the curve if he rewinds.
  375.     //
  376.     if( $setInitialState ) {
  377.         SetInitialState;
  378.     }
  379.  
  380.     waitCursor -state off;
  381. }
  382.